package enron;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import javax.swing.table.TableCellRenderer;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import enron.Generator.Generator;

public class GeneratorCellRenderer extends JComponent 
implements TableCellRenderer {
	private static final long serialVersionUID = 4996620028454024439L;
	private JPanel cellPanel = new JPanel();
	private JLabel name = new JLabel("<replace>");
	private JLabel valueDisplay = new JLabel("1000Mw");
	private JSlider slider = new JSlider();
	private Generator myGenerator = null;
	
	public GeneratorCellRenderer() {
		super();
		cellPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.PREF_COLSPEC,
				FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("225px:grow"),},
			new RowSpec[] {
				FormFactory.PREF_ROWSPEC,}));
//		name.setBorder(new EmptyBorder(0,0,0,0));
		name.setBorder(new LineBorder(Color.red));
		name.setOpaque(false);
		cellPanel.add(name, "1, 1, fill, center");
		

		valueDisplay.setName("valueDisplay");
//		valueDisplay.setBorder(new EmptyBorder(0,0,0,0));
		valueDisplay.setBorder(new LineBorder(Color.yellow));
		valueDisplay.setOpaque(false);
		valueDisplay.setMinimumSize(valueDisplay.getSize());
		cellPanel.add(valueDisplay, "3, 1, default, center");
		
//		slider.setPaintLabels(true);
		slider.setPaintTicks(true);
//		slider.setBorder(new EmptyBorder(0,0,0,0));
		slider.setBorder(new LineBorder(Color.green));
		slider.setOpaque(false);
//		slider.putClientProperty("valueDisplay", valueDisplay);
//		slider.addChangeListener(new ChangeListener() {
//			public void stateChanged(ChangeEvent event) {
//				JLabel valueDisplay = (JLabel) ((JComponent) event.getSource()).getClientProperty("valueDisplay");
//				valueDisplay.setText(Double.toString(((JSlider) event.getSource()).getValue()/1000.0)+"Mw");
//			}
//		});
		cellPanel.setBorder(new LineBorder(Color.pink));
		cellPanel.add(slider, "5, 1, fill, fill");
		//cellPanel.setSize(cellPanel.getWidth(), slider.getHeight());
	}
	
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value, 
			boolean isSelected, boolean hasFocus, int row, int column) {
		if(value != null && Generator.class.isInstance(value)) {
			myGenerator = (Generator) value;
			int operatingRange = (myGenerator.getMaximumSupply().intValue()/1000) - ((myGenerator.getMinimumSupply().intValue()/1000) - 1);
			slider.setMajorTickSpacing(operatingRange/5);
			slider.setMinorTickSpacing(operatingRange/25);
			slider.setMaximum(myGenerator.getMaximumSupply().intValue()/1000);
			slider.setMinimum(myGenerator.getMinimumSupply().intValue()/1000 - 1);
			if(myGenerator.getCurrentSupply() >= myGenerator.getMinimumSupply()) {
				slider.setValue(myGenerator.getCurrentSupply().intValue()/1000);
			} else {
				slider.setValue(myGenerator.getMinimumSupply().intValue()/1000 - 1);
			}
			name.setText(myGenerator.getDisplay());
		} else {
			myGenerator = null;
			slider.setMajorTickSpacing(0);
			slider.setMinorTickSpacing(0);
			slider.setMinimum(0);
			slider.setMaximum(1);
			slider.setValue(1);
			name.setText("{null}");
		}
		valueDisplay.setText(Double.toString(slider.getValue()/1000.0)+"Mw");
//		cellPanel.setSize(slider.getWidth() + name.getWidth()+5,slider.getHeight());
		//cellPanel.setSize(cellPanel.getWidth(),slider.getHeight());
		return cellPanel;
	}

}
